package com.geekband.Test01;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import java.util.HashMap;

/**
 * Function:
 * Create date on 15/11/26.
 *
 * @author Conquer
 * @version 1.0
 */
public class WebViewButtonActivity  extends AppCompatActivity {

    private WebView mWebView;


    public class TestJSEvent{

        @JavascriptInterface
        public void showToast(String toast){
            Toast.makeText(WebViewButtonActivity.this, toast, Toast.LENGTH_LONG).show();
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        mWebView = (WebView) findViewById(R.id.web_view);

        mWebView.setWebContentsDebuggingEnabled(true);

        mWebView.loadUrl("http://www.baidu.com");
        HashMap<String,String> map = new HashMap<>();
        map.put("token","xxxxxx");
        map.put("my_header","header");
        mWebView.loadUrl("http://www.baidu.com", map);

//        mWebView.loadUrl("file:///android_asset/test.html");

        mWebView.getSettings().setJavaScriptEnabled(true);


        // JS调用原生App
        mWebView.addJavascriptInterface(new TestJSEvent(), "TestApp");

        // 原生App调用JS

        mWebView.loadUrl("javascript:javaCallJS('')");



        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieManager.setCookie("domain", "cookie");

        mWebView.setWebViewClient(new WebViewClient(){

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 是不是重新加载 （拦截见面）
                // 404页面 ————

                if(url.contains("404")){
                }

//                view.loadUrl("http://www.zhihu.com");

                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // 页面开始：显示loading动画
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // 页面开始：隐藏loading动画
                super.onPageFinished(view, url);
            }

            @Override
            public void onLoadResource(WebView view, String url) {

                // url 替换
                if(url.contains("logo.img")){

                }
                super.onLoadResource(view, url);
            }

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String url) {

                return super.shouldInterceptRequest(view, url);
            }

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                // request        hybrid 离线网页
                return super.shouldInterceptRequest(view, request);
            }

        });


        mWebView.setWebChromeClient(new TestWebChromeClient());
    }

    public class TestWebChromeClient extends WebChromeClient{
        public TestWebChromeClient() {
            super();
        }

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
        }

        @Override
        public void onReceivedIcon(WebView view, Bitmap icon) {
            super.onReceivedIcon(view, icon);
        }

        @Override
        public void onReceivedTouchIconUrl(WebView view, String url, boolean precomposed) {
            super.onReceivedTouchIconUrl(view, url, precomposed);
        }

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            super.onShowCustomView(view, callback);
        }

        @Override
        public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
            super.onShowCustomView(view, requestedOrientation, callback);
        }
    }

    @Override
    public void onBackPressed() {
        if(mWebView != null && mWebView.canGoBack()){

//            WebBackForwardList webBackForwardList = mWebView.copyBackForwardList();
//
//            WebHistoryItem historyItem = webBackForwardList.getItemAtIndex(0);
//
//            String historyUrl = historyItem.getUrl();

            mWebView.goBack();
            mWebView.goForward();
            mWebView.goBackOrForward(+1);
            mWebView.reload();
        } else {
            super.onBackPressed();
        }
    }
}
